package Mysoftwork;

import java.io.*;
import java.util.*;

public class WordCount {
	public static String resultPath = "/Users/zhangyilin/IdeaProjects/softwork2/src/result.txt";
    public static List<String> lists = new ArrayList<>();

    public static void main(String[] args) throws Exception {
        Scanner input = new Scanner(System.in);
        Map<String, Integer> treeMap = new TreeMap<String, Integer>();
        System.out.println("请输入文件名：");
        String path = input.next();
//        int wordsNum = 0;//单词总数
//        InputStreamReader isr = new InputStreamReader(new FileInputStream(path));// 建立一个输入流对象
//        BufferedReader br = new BufferedReader(isr);
//        String str = br.readLine();
//        isr.close();//关闭
        System.out.println(charCount(path));
        System.out.println(wordCount(path));
        System.out.println(lineCount(path));
        for (int i = 0; i < lists.size(); i++) {
            String s = lists.get(i).toLowerCase();
            if (!treeMap.containsKey(s)) {
                treeMap.put(s, 1);
            } else {
                int n = treeMap.get(s);
                treeMap.put(s, n + 1);
            }
        }
        System.out.println(sortWord(treeMap));
        writefile(resultPath,charCount(path)+"\n"+wordCount(path)+"\n"+
                lineCount(path)+"\n"+sortWord(treeMap));

//
    }

    public static String sortWord(Map<String, Integer> treeMap) throws Exception{
        List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(treeMap.entrySet());
        Collections.sort(list, ((o1, o2) -> o2.getValue().compareTo(o1.getValue())));
        int n = 10;
        String s[] = new String[n];

        int i =0;
        for (Map.Entry<String, Integer> entry : list) {
            if (n-- == 0)
                break;
            String key = entry.getKey();
            Integer value = entry.getValue();
            s[i] = "<" + key + ">:" + value;
//            System.out.print("<" + key + ">:" + value);
//            System.out.println(s[i]);
            i++;
        }
        String string = s[0]+"\n";
        for (int j = 1; j <i ; j++) {
            string =string+ s[j]+"\n";
        }
        return string;

    }

    public static String charCount(String path) throws Exception {
        int charNum = 0;
        InputStreamReader isr = new InputStreamReader(new FileInputStream(path));// 建立一个输入流对象
        BufferedReader br = new BufferedReader(isr);
        int getchar;
        while ((getchar = br.read()) != -1) {
            //忽略汉字，换行也算字符
            if (getchar > 31 && getchar < 127 || getchar == 10) {
                charNum++;
            }
        }
        return "characters: " + charNum;
    }

    public static void writefile(String path, String content) throws Exception {
        try {
            OutputStream out = new FileOutputStream(path);
            out.write(content.getBytes());
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static String lineCount(String path)throws Exception{
        int lineNum = 0;
        InputStreamReader isr = new InputStreamReader(new FileInputStream(path));// 建立一个输入流对象
        BufferedReader br = new BufferedReader(isr);
        String str = br.readLine();
        while (str != null) {//统计有效行的字符
            lineNum++;
            str = br.readLine();
        }

        return "lines:" + lineNum;
    }
    public static String wordCount(String path)throws Exception{
        int wordsNum = 0;//单词总数
        InputStreamReader isr = new InputStreamReader(new FileInputStream(path));// 建立一个输入流对象
        BufferedReader br = new BufferedReader(isr);
        String str = br.readLine();
        while (str != null) {//统计有效行的字符
            String wordsArr[] = str.split("\\s*[^0-9a-zA-Z]+");//根据分隔符为数字字母以外的存放在数组
            for (String word : wordsArr) {
                //以4个英文字母开头，跟上字母数字符号
                if (word.matches("[a-zA-Z]{4,}[a-zA-Z0-9]*")) {
                    lists.add(word);
                }
            }
            str = br.readLine();
        }
        wordsNum = lists.size();

        return "words:" + wordsNum;
    }


}
